package com.rulai.generator.plugin.repository;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.ObjectUtils;

import com.rulai.generator.framework.util.JdbcUtil;
import com.rulai.generator.framework.util.StringUtil;
import com.rulai.generator.plugin.GeneratorRepository;
import com.rulai.generator.plugin.model.Table2Model;

/**
 * jdbc存储层抽象 <功能详细描述>
 * 
 * @since [产品/模块版本]
 * @author leiyang@qding.me
 * @version v1.0 Date:2018年5月8日 下午3:19:27
 */
public class JdbcGeneratorRepository implements GeneratorRepository {

    /**
     * jdbcDriver jdbc驱动
     */
    private String jdbcDriver;

    /**
     * jdbcUrl jdbc url
     */
    private String jdbcUrl;

    /**
     * jdbcUsername 用户名
     */
    private String jdbcUsername;

    /**
     * jdbcPassword 密码
     */
    private String jdbcPassword;

    /**
     * database 数据库名
     */
    private String database;

    /**
     * tablePrefix 表前缀
     */
    private String tablePrefix;

    public JdbcGeneratorRepository(String jdbcDriver, String jdbcUrl, String jdbcUsername, String jdbcPassword,
            String database, String tablePrefix) {
        
        this.jdbcDriver=jdbcDriver;
        this.jdbcUrl=jdbcUrl;
        this.jdbcUsername=jdbcUsername;
        this.jdbcPassword=jdbcPassword;
        this.database=database;
        this.tablePrefix=tablePrefix;

    }

    @Override
    public List<Table2Model> getTableModels() {

        String sql = "";
        if (StringUtil.isBlank(tablePrefix)) {

            sql = "SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = '" + database + "' ;";

        } else {
            sql = "SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = '" + database
                    + "' AND table_name LIKE '" + tablePrefix + "_%';";

        }

        List<Table2Model> tables = new ArrayList<Table2Model>();
        // 查询定制前缀项目的所有表
        JdbcUtil jdbcUtil = new JdbcUtil(jdbcDriver, jdbcUrl, jdbcUsername, jdbcPassword);
        List<Map> result = null;
        try {
            result = jdbcUtil.selectByParams(sql, null);
        } catch (SQLException e) {
            System.out.println("查询数据库出错!!");
            e.printStackTrace();
            return tables;
        }
        for (Map map : result) {
            System.out.println(map.get("TABLE_NAME"));

            String talbename = String.valueOf(map.get("TABLE_NAME"));
            String modelname = StringUtil.lineToHump(ObjectUtils.toString(map.get("TABLE_NAME")));
            Table2Model table2model = new Table2Model();
            table2model.setTableName(talbename);
            table2model.setModelName(modelname);
            tables.add(table2model);
        }
        jdbcUtil.release();
        return tables;
    }

    public String getJdbcDriver() {
        return jdbcDriver;
    }

    public void setJdbcDriver(String jdbcDriver) {
        this.jdbcDriver = jdbcDriver;
    }

    public String getJdbcUrl() {
        return jdbcUrl;
    }

    public void setJdbcUrl(String jdbcUrl) {
        this.jdbcUrl = jdbcUrl;
    }

    public String getJdbcUsername() {
        return jdbcUsername;
    }

    public void setJdbcUsername(String jdbcUsername) {
        this.jdbcUsername = jdbcUsername;
    }

    public String getJdbcPassword() {
        return jdbcPassword;
    }

    public void setJdbcPassword(String jdbcPassword) {
        this.jdbcPassword = jdbcPassword;
    }

    public String getDatabase() {
        return database;
    }

    public void setDatabase(String database) {
        this.database = database;
    }

    public String getTablePrefix() {
        return tablePrefix;
    }

    public void setTablePrefix(String tablePrefix) {
        this.tablePrefix = tablePrefix;
    }

}
